<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select v-model="queryParams.customerType" placeholder="请选择客户类型" clearable>
          <el-option
            v-for="dict in dict.type.crm_company_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证编码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入认证编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户行业" prop="customerField">
        <el-select v-model="queryParams.customerType" placeholder="请选择客客户行业" clearable>
          <el-option
            v-for="dict in dict.type.crm_company_field"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="liaison">
        <el-input
          v-model="queryParams.liaison"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="liaisonPhone">
        <el-input
          v-model="queryParams.liaisonPhone"
          placeholder="请输入联系人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.crm_company_process"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小程序认证码" prop="miniAppCode">
        <el-input
          v-model="queryParams.miniAppCode"
          placeholder="请输入小程序认证码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['net:crmCompany:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['net:crmCompany:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['net:crmCompany:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['net:crmCompany:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="crmCompanyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="客户类型" align="center" prop="customerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_company_type" :value="scope.row.customerType"/>
        </template>
      </el-table-column>
      <el-table-column label="认证编码" align="center" prop="customerCode"/>
      <el-table-column label="客户行业" align="center" prop="customerField">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_company_field" :value="scope.row.customerField"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="liaison"/>
      <el-table-column label="联系人手机号" width="140px" align="center" prop="liaisonPhone"/>
      <el-table-column label="客户账号状态" width="140px" align="center" prop="customerStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.customerStatus == 1 ? '停用' : '启用' }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="收益费率" align="center" prop="revenueRate"/>-->
      <el-table-column label="审核状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_company_process" :value="scope.row.processStatus"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="是否可以登录" width="140px" align="center" prop="canLogin">
        <template slot-scope="scope">
          <span>{{ scope.row.canLogin == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="小程序认证状态" width="140px" align="center" prop="miniAppStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.miniAppStatus == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="小程序认证时间" align="center" prop="miniAppTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.miniAppTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="小程序账号" width="140px" align="center" prop="miniAppId"/>
      <el-table-column label="银行名称" width="180px" align="center" prop="bankName"/>
      <el-table-column label="卡号" width="180px" align="center" prop="bankAccount"/>
      <el-table-column label="银行卡人名" width="140px" align="center" prop="bankUserName"/>
      <el-table-column label="开户行" width="160px" align="center" prop="bankAddress"/>
      <el-table-column label="操作" align="center" width="180" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['net:crmCompany:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            @click="handleResetPassword(scope.row)"
            v-hasPermi="['net:crmCompany:resetPassword']"
          >重置密码
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            v-if="scope.row.customerStatus == 0"
            @click="handleBanAccount(scope.row)"
            v-hasPermi="['net:crmCompany:disabledAccount']"
          >停用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-if="scope.row.customerStatus == 1"
            @click="handleUpAccount(scope.row)"
            v-hasPermi="['net:crmCompany:enabledAccount']"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['net:crmCompany:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户类型" prop="customerType">
              <el-select v-model="form.customerType" placeholder="请选择客户类型">
                <el-option
                  v-for="dict in dict.type.crm_company_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="认证编码" prop="customerCode">
              <el-input v-model="form.customerCode" placeholder="请输入认证编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户行业" prop="customerField">
              <el-input v-model="form.customerField" placeholder="请输入客户行业"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系人" prop="liaison">
              <el-input v-model="form.liaison" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号" prop="liaisonPhone">
              <el-input v-model="form.liaisonPhone" placeholder="请输入联系人手机号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="收益费率" prop="revenueRate">
              <el-input v-model="form.revenueRate" placeholder="请输入收益费率"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态">
              <el-radio-group v-model="form.processStatus">
                <el-radio
                  v-for="dict in dict.type.crm_company_process"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="是否可以登录" prop="canLogin">
              <el-input v-model="form.canLogin" placeholder="请输入是否可以登录"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="customerAccount">
              <el-input v-model="form.customerAccount" placeholder="请输入登录账号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="银行名称" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入银行名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入卡号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="银行卡人名" prop="bankUserName">
              <el-input v-model="form.bankUserName" placeholder="请输入银行卡人名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户行" prop="bankAddress">
              <el-input v-model="form.bankAddress" placeholder="请输入开户行"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCrmCompany, getCrmCompany, delCrmCompany, addCrmCompany, updateCrmCompany, resetCompanyPassword, disabledAccount, enabledAccount} from "@/api/net/crmCompany";

export default {
  name: "CrmCompany",
  dicts: ['crm_company_type', 'crm_company_field', 'crm_company_process'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      crmCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        customerName: null,
        customerType: null,
        customerCode: null,
        customerField: null,
        liaison: null,
        liaisonPhone: null,
        customerStatus: null,
        revenuePrice: null,
        withdrawAmount: null,
        todayRevenuePrice: null,
        revenueRate: null,
        processStatus: null,
        canLogin: null,
        customerAccount: null,
        miniAppStatus: null,
        miniAppTime: null,
        miniAppId: null,
        miniAppCode: null,
        bankName: null,
        bankAccount: null,
        bankUserName: null,
        bankAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCrmCompany(this.queryParams).then(response => {
        this.crmCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        customerName: null,
        customerType: null,
        customerCode: null,
        customerField: null,
        liaison: null,
        liaisonPhone: null,
        customerStatus: 0,
        revenuePrice: null,
        withdrawAmount: null,
        todayRevenuePrice: null,
        revenueRate: null,
        processStatus: "0",
        canLogin: null,
        miniAppStatus: 0,
        miniAppTime: null,
        miniAppId: null,
        miniAppCode: null,
        bankName: null,
        bankAccount: null,
        bankUserName: null,
        bankAddress: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCrmCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCrmCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCrmCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户管理编号为"' + ids + '"的数据项？').then(function () {
        return delCrmCompany(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('net/crmCompany/export', {
        ...this.queryParams
      }, `crmCompany_${new Date().getTime()}.xlsx`)
    },
    /** 重置密码 */
    handleResetPassword(row) {
      resetCompanyPassword(row.id).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess("密码重置成功");
        }
      });
    },
    /** 停用账号 */
    handleBanAccount(row) {
      // 调接口
      disabledAccount(row.id).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess("账号停用成功");
          this.getList();
        }
      });
    },
    /** 启用账号 */
    handleUpAccount(row) {
      // 调接口
      enabledAccount(row.id).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess("账号启用成功");
          this.getList();
        }
      });
    }
  }
};
</script>
